package com.example.modelfunction.test.redisAndCacheTest;

import com.example.base.Cache.Cache;
import com.example.modelfunction.shrio.usermanager.mode.User;
import com.example.modelfunction.shrio.usermanager.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

public class redisOp {
    @Autowired
    Cache myCache;
    @Autowired
    private UserRepository userMapper;
    @RequestMapping(value = "/gredis",method = RequestMethod.GET)
    public String  getRedis(String key){
        return (String)myCache.get(key);
    }
    @RequestMapping(value = "/sredis",method = RequestMethod.GET)
    public void  setRedis(String key,String value){
        myCache.set(key,value);
    }

    //@Cacheable(cacheNames = "product1",key = "#root.methodName+'['+#id+']'")
    //@Cacheable(cacheNames = {"product1","product2"})// 默认key为参数，多个参数SimpleKey [arg1,arg2]
    //@Cacheable(cacheNames = "product",key = "#root.methodName+'['+#id+']'")
    //@Cacheable(cacheNames = "product",keyGenerator = "myKeyGenerator")
    //@Cacheable(cacheNames = "product",key = "#root.methodName+'['+#id+']'",condition="#a0>10",unless = "#a0==11") //或者condition="#id>10")
    //@CachePut(value="product",key = "#result.productId",condition = "#result!=null")
    // @CacheEvict(value="product",allEntries = true,beforeInvocation = true)
    @Cacheable(value = "usercache", keyGenerator = "keyGenerator")
    @RequestMapping(value = "/useraccount",method = RequestMethod.GET)
    public User getUserName(String token){
        /* User user=userMapper.findByAccount(token);
        redis.set(user.cacheKey(),user);
        User muser=(User)redis.get(user.cacheKey());*/
        return userMapper.findByAccount(token);
    }
    //@CacheEvict(value="usercache",allEntries = true,beforeInvocation = true)
    @CacheEvict(value = "usercache", keyGenerator = "keyGenerator",beforeInvocation = true)
    @RequestMapping(value = "/useraccount",method = RequestMethod.GET)
    public void removeuser(String token){


    }
}
